package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.ListQueryId;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserLoginVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.RequestToViewNameTranslator;

import javax.swing.plaf.PanelUI;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@RestController
@Tag(name = "车辆管理")
@RequestMapping("v1/vehicle")
public class VehicleController {
    @Autowired
    private VehicleMapper mapper;
    @Autowired
    private RequestToViewNameTranslator viewNameTranslator;

    @PostMapping("/addVehicle")
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    public JsonResult addVehicle(@RequestBody VehicleAddParam param, HttpSession session)
    {
        log.debug("param:"+param);
        UserLoginVo userLoginVo=(UserLoginVo) session.getAttribute("user");
        //登录状态校验已经在拦截器中完成
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(param,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setId(1L);
        vehicle.setUserId(userLoginVo.getId());
        log.debug("vehicle:"+vehicle);
        int x=mapper.insert(vehicle);
        return JsonResult.ok();
    }

    @Operation(summary = "车辆列表")
    @GetMapping("/vehicleList")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand"),
            @Parameter(name = "license"),
            @Parameter(name = "vehicleListQuery", hidden = true),
    })
    public JsonResult vehicleList(VehicleListQuery query,HttpSession session)
    {
        log.debug("query:"+query);
        UserLoginVo userLoginVo = (UserLoginVo)session.getAttribute("user");

        ListQueryId qid=new ListQueryId();
        BeanUtils.copyProperties(query,qid);

        log.debug("----------------");
        log.debug("----------------");
        log.debug("----------------");
        log.debug("userLoginVo.getId()"+userLoginVo.getId());
        System.out.println("------------");
        System.out.println("------------");
        System.out.println("------------");
        System.out.println("userLoginVo.getId()"+userLoginVo.getId());
        System.out.println("------------");
        System.out.println("------------");
        System.out.println("------------");
        System.out.println("userLoginVo.getId()"+userLoginVo.getId());
        qid.setUserId(userLoginVo.getId());
        List<Vehicle> list = mapper.getlist(qid);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,list);
    }

    @Operation(summary = "车辆删除")
    @GetMapping("/vehicleDelete/{id}" )
    @ApiOperationSupport(order = 30)
    public JsonResult vehicleDelete(@PathVariable @Range(min = 1L,max = 500L,message = "必须在1-500之间") Long id, HttpSession session)
    {
        if(id<0)
            throw new IllegalArgumentException("id<0");
        UserLoginVo userLoginVo = (UserLoginVo)session.getAttribute("user");

        Long userId= userLoginVo.getId();
        int x=mapper.delete(id,userId);
        if(x>0)
            return JsonResult.ok();
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}





















